"""
分组页面类
功能: 包含分组页面元素和部分函数化操作, 如: 添加分组, 等
"""

from Page.PageObject.HomePage import HomePage
import time


class MenuAdmin(HomePage):
    # 从配置文件获取元素
    button_add_group = HomePage.cf.getLocatorsOrAccount('menu_admin', 'button_add_group')
    input_add_group = HomePage.cf.getLocatorsOrAccount('menu_admin', 'input_add_group')

    button_confirm = HomePage.cf.getLocatorsOrAccount('menu_admin', 'button_confirm')
    button_cancel = HomePage.cf.getLocatorsOrAccount('menu_admin', 'button_cancel')

    button_add_to_group = HomePage.cf.getLocatorsOrAccount('menu_admin', 'button_add_to_group')
    input_Description = HomePage.cf.getLocatorsOrAccount('menu_admin', 'input_Description')
    button_Search = HomePage.cf.getLocatorsOrAccount('menu_admin', 'button_Search')
    span_select_all = HomePage.cf.getLocatorsOrAccount('menu_admin', 'span_select_all')
    button_confirm_dialog = HomePage.cf.getLocatorsOrAccount('menu_admin', 'button_confirm_dialog')

    # 企业信息
    ServerURL = HomePage.cf.getLocatorsOrAccount('menu_Enterprise', 'ServerURL')
    AuthenticationMode = HomePage.cf.getLocatorsOrAccount('menu_Enterprise', 'AuthenticationMode')

    def get_Enterprise_channel_info(self):
        """
        在企业信息页面获取上报url
        :return: onu router olt的上报url list
        """
        list1 = []
        list2 = []
        # url
        ele = self.findElements(*self.ServerURL)
        if len(ele) == 3:
            for i in ele:
                list1.append(i.text)

        # auth
        ele = self.findElements(*self.AuthenticationMode)
        if len(ele) == 3:
            for i in ele:
                list2.append(i.text)
        info_list = list(zip(list1, list2))
        print(info_list)
        return info_list

    def get_group_element(self, group_name):
        # 检查分组是否已存在, 返回分组所在span元素

        ele = self.findElement('xpath', f'//span[@class="custom-tree-node" and ./span[text()="{group_name}"]]')
        if ele:
            return ele
        else:
            return False

    def Add_Group(self, group_name):
        """
        添加用户组功能

        此函数通过模拟用户交互，完成在管理员菜单中添加用户组的操作

        参数:
        - group_name (str): 需要添加的用户组名称

        返回值: 无
        """
        # 点击添加用户组按钮
        self.click(*MenuAdmin.button_add_group)

        # 输入用户组名称
        self.sendKeys(*MenuAdmin.input_add_group, group_name)

        # 点击确认按钮完成添加
        self.click(*MenuAdmin.button_confirm)

    def Delete_Group(self, group_name):
        ele = self.get_group_element(group_name)
        if ele:
            ele.click()
        else:
            print(f'分组不存在')

        # 点击删除
        self.click('xpath',
                   '//span[text()="Test_group"]/following-sibling::span[1]//i[contains(@class, \'icon-delete-bin-line\')]')

        # 点击确认按钮
        ele = self.findElements('xpath', '//button[./span[normalize-space(text())="Confirm"]]')
        for i in ele:
            if i.is_displayed():
                i.click()
                break

    def Add_device_to_group(self, device_name, group_name):
        ele = self.get_group_element(group_name)
        if ele:
            ele.click()
            # print('分组存在')
        else:
            print(f'分组不存在, Add_device_to_group, Error, group name: {group_name} not found!')
            # return False
        self.click(*MenuAdmin.button_add_to_group)
        self.sendKeys(*MenuAdmin.input_Description, device_name)
        self.click(*MenuAdmin.button_Search)
        time.sleep(1)
        # 等待搜索完毕
        self.click(*MenuAdmin.span_select_all)
        self.click(*MenuAdmin.button_confirm_dialog)

    def assert_device_exist(self, device_name, group_name):
        ele = self.get_group_element(group_name)
        if ele:
            ele.click()
            # print('分组存在')
        else:
            print(f'分组不存在, Add_device_to_group, Error, group name: {group_name} not found!')
            assert False
        # by = 'class'
        # //div[@class="grouping-right"]//span[text()="DF276A1C6BDC"]
        # //div[@class="grouping-right"]//span[text()="DF276A1C6BDC"]
        location = f'//div[@class="grouping-right"]//span[text()="{device_name}"]'
        tmp_tuple = ('xpath', location)
        ele = self.isElementExsit(*tmp_tuple)
        if ele:
            print('assert 设备在分组内')
            pass
            # 继续进行下面的步骤
        else:
            print('assert 设备不在分组内')
            assert False
        # assert self.isElementExsit(*tmp_tuple)


if __name__ == "__main__":
    from selenium import webdriver
    from selenium.webdriver.common.by import By

    options = webdriver.ChromeOptions()
    options.add_experimental_option("excludeSwitches", ['enable-automation', 'load-extension'])  # 屏蔽自动化受控提示 && 开发者提示
    options.add_argument("--start-maximized")  # 浏览器启动时窗口最大化
    options.add_experimental_option("prefs", {"credentials_enable_service": False,
                                              "profile.password_manager_enabled": False})
    driver = webdriver.Chrome(options=options)

    menu_admin = MenuAdmin(driver, 6)
    print(f'url = {menu_admin.cms_url}')

    menu_admin.login_cms()
    print('login finish 1')

    menu_admin.cd_Admin()

    tmp_li = menu_admin.get_Enterprise_channel_info()
    print(tmp_li)